import { ref } from 'vue'
import { defineStore } from 'pinia'
import { getLoginUser, userLogout } from '@/api/userController'

const DEFAULT_LOGIN_USER:API.LoginUserVO = {
  username: '未登录',
}

export const useLoginUserStore = defineStore('loginUser', () => {
  const loginUser = ref<API.LoginUserVO>(DEFAULT_LOGIN_USER)

  const fetchLoginUser = async () => {
    const res = await getLoginUser()
    if (res.code === 0) {
      loginUser.value = res.data
    }
  }

  const logoutUser = async () => {
    const res = await userLogout()
    if (res.code === 0) {
      loginUser.value = DEFAULT_LOGIN_USER
    }
  }

  return { loginUser, fetchLoginUser, logoutUser }
})
